<?php
namespace app\widget;
use think\template\TagLib;
class Tag extends TagLib {
    
    protected $tags   =  [
        'nav'      => ['close' => 1], 
        'friendlink'      => ['close' => 0], 
        'ad'      => ['close' => 0], 
    ];
    
    public function tagNav($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'val';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                }
            }
        }
        $html  = "<?php ";
        $html .= '$__think__tag__ = \'' . json_encode($tag) . '\';';
        $html .= '$__think__list__ = "";';
        $html .= '$__think__list__ = model("app.widget.model.WidgetNav")->getItemListByTag($__think__tag__);';
        $html .= ' ?>';
        $html .= '{volist name="__think__list__" id="' . $tag['id'] . '" key="' . $tag['key'] . '"}';
        $html .= $content;
        $html .= '{/volist}';
        return $html;
    }

    public function tagFriendlink($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'val';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                }
            }
        }
        
        $html  = "<?php ";
        $html .= '$__think__tag__ = \'' . json_encode($tag) . '\';';
        $html .= '$__think__list__ = "";';
        $html .= '$__think__list__ = model("app.widget.model.WidgetFriendLink")->getItemListByTag($__think__tag__);';
        $html .= '$'. $tag['id'] .' = $__think__list__; ?>';
        return $html;
    }
    
    public function tagAd($tag, $content)
    {
        
        if (!isset($tag['id'])) {
            $tag['id'] = 'val';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                    
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__data = "";';
        $html .= '$__think__data = model("app.widget.model.WidgetAd")->getItemListByTag($__think__tag__);';
        $html .= 'echo $__think__data; ?>';
        return $html;
    }
}
